/***********************************************************
 * @Description : 对外REST接口
 * @author      : 梁山广(Laing Shan Guang)
 * @date        : 2019-05-16 23:45
 * @email       : liangshanguang2@gmail.com
 ***********************************************************/
package lsgwr.exam.controller;

import cn.hutool.core.codec.Base64;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lsgwr.exam.dto.RegisterDTO;
import lsgwr.exam.entity.User;
import lsgwr.exam.enums.ResultEnum;
import lsgwr.exam.qo.LoginQo;
import lsgwr.exam.service.SysUserService;
import lsgwr.exam.service.UserService;
import lsgwr.exam.utils.R;
import lsgwr.exam.vo.FormVo;
import lsgwr.exam.vo.ResultVO;
import lsgwr.exam.vo.UserInfoVo;
import lsgwr.exam.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@Api(tags = "System User APIs")
@RequestMapping("/api/system/user")
public class SystemUserController {

    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/list")
    @ApiOperation("获取用户列表")
    ResultVO<List<UserVo>> getUserList() {
        List<UserVo> userVoList = sysUserService.getUserList();
        return new ResultVO<>(ResultEnum.GET_INFO_SUCCESS.getCode(), ResultEnum.GET_INFO_SUCCESS.getMessage(), userVoList);
    }

    @GetMapping("{id}")
    @ApiOperation("获取单个用户")
    ResultVO<UserVo> getUserById(@PathVariable("id") String id) {
        System.out.println("进入/user/info的获取用户信息的接口"+id);
        UserVo userVo = sysUserService.getInfo(id);
        return new ResultVO<>(ResultEnum.GET_INFO_SUCCESS.getCode(), ResultEnum.GET_INFO_SUCCESS.getMessage(), userVo);
    }


    @PostMapping("")
    ResultVO<User> saveUser(@RequestBody UserVo userVoData){
        User user = new User();
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        user.setUserId(uuid);
        user.setUserUsername(userVoData.getUserUsername());
        user.setUserNickname(userVoData.getUserUsername());
        user.setUserPassword(Base64.encode(userVoData.getPassword(), StandardCharsets.UTF_8));
        user.setUserPhone(userVoData.getUserPhone());
        user.setUserRoleId(userVoData.getUserRoleId());
        user.setUserAvatar("https://i.loli.net/2019/11/02/rCHKVJd4jTovzW9.jpg");
        //user.setUserDescription("好好学习，天天向上");
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        sysUserService.saveUser(user);
        return new ResultVO<>(ResultEnum.GET_INFO_SUCCESS.getCode(), ResultEnum.GET_INFO_SUCCESS.getMessage(), null);
    }

    @PutMapping("")
    ResultVO<User> updateUser(@RequestBody UserVo userVoData){
        userVoData.setUpdateTime(new Date());
        sysUserService.updateUser(userVoData);
        return new ResultVO<>(ResultEnum.GET_INFO_SUCCESS.getCode(), ResultEnum.GET_INFO_SUCCESS.getMessage(), null);
    }

    @DeleteMapping()
    ResultVO<User> deleteUser(@RequestBody UserVo userVo){
        String id = userVo.getUserId();
        sysUserService.deleteUser(id);
        return new ResultVO<>(ResultEnum.GET_INFO_SUCCESS.getCode(), ResultEnum.GET_INFO_SUCCESS.getMessage(), null);
    }
}
